package com.sikao.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sikao.app.R;
import com.sikao.app.bean.MallGoodsDetailBean;
import com.sikao.app.utils.Utils;

import java.util.ArrayList;
import java.util.List;

public class MlvMallOrderExtrasItemAdapter extends BaseAdapter {

	private List<MallGoodsDetailBean.ResBean.GoodsAttrsBean> objects = new ArrayList<MallGoodsDetailBean.ResBean.GoodsAttrsBean>();

	private Context context;
	private LayoutInflater layoutInflater;
	private MlvMallOrderExtrasItemAdapterOnClickListener mlvMallOrderExtrasItemAdapterOnClickListener;

	public MlvMallOrderExtrasItemAdapter(Context context, MlvMallOrderExtrasItemAdapterOnClickListener mlvMallOrderExtrasItemAdapterOnClickListener, List<MallGoodsDetailBean.ResBean.GoodsAttrsBean> objects) {
		this.context = context;
		this.layoutInflater = LayoutInflater.from(context);
		this.mlvMallOrderExtrasItemAdapterOnClickListener = mlvMallOrderExtrasItemAdapterOnClickListener;
		this.objects = objects;


	}

	@Override
	public int getCount() {
		return objects.size();
	}

	@Override
	public MallGoodsDetailBean.ResBean.GoodsAttrsBean getItem(int position) {
		return objects.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		//if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.mlv_mall_order_extras_item, null);
			convertView.setTag(new ViewHolder(convertView));
		//}
		initializeViews(position, objects.get(position), (ViewHolder) convertView.getTag());

		//只有一行不显示
		Utils.showLog("===数据数量 position："+position+" size:"+objects.get(position).getDefault_value().size() +" ");
		if (objects.get(position).getDefault_value().size()==1){
			Utils.showLog("===数据只有一项");
			((ViewHolder) convertView.getTag()).rl_mall_order_extras_all.setVisibility(View.GONE);
		}

		return convertView;
	}

	private void initializeViews(int position, MallGoodsDetailBean.ResBean.GoodsAttrsBean goodsAttrsBean, ViewHolder holder) {
		holder.rl_mall_order_extras_all.setTag(position);
		holder.rl_mall_order_extras_all.setOnClickListener(mlvMallOrderExtrasItemAdapterOnClickListener);

		holder.tvMallOrderExtrasName.setText(goodsAttrsBean.getName());
		holder.tvMallOrderExtrasContent.setText(goodsAttrsBean.getContent());
	}


	protected class ViewHolder {
		private TextView tvMallOrderExtrasName;
		private TextView tvMallOrderExtrasContent;
		private RelativeLayout rl_mall_order_extras_all;

		public ViewHolder(View view) {
			tvMallOrderExtrasName = (TextView) view.findViewById(R.id.tv_mall_order_extras_name);
			tvMallOrderExtrasContent = (TextView) view.findViewById(R.id.tv_mall_order_extras_content);
			rl_mall_order_extras_all = (RelativeLayout) view.findViewById(R.id.rl_mall_order_extras_all);
		}
	}

	//点击回调
	public static abstract class MlvMallOrderExtrasItemAdapterOnClickListener implements View.OnClickListener {
		@Override
		public void onClick(View v) {
			myOnClick((Integer) v.getTag());
		}

		public abstract void myOnClick(int position);
	}
}

